﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace SpaceGame.Gameplay.AI.Pathfinding
{
    /// <summary>
    /// Simple decorator class that allows different heuristics to be blended.
    /// </summary>
    public class EuclideanPathHeuristic : IPathHeuristic
    {
        protected IPathHeuristic next;
        protected Func<float, float, float> blendOperator;


        /// <summary>
        /// Creates basic path heuristic that only uses euclidian distance as estimation.
        /// </summary>
        public EuclideanPathHeuristic()
        {
        }

        /// <summary>
        /// Creates new simple heuristic that blends result from other heuristic for estimation.
        /// </summary>
        /// <param name="blendOperator"></param>
        /// <param name="next"></param>
        public EuclideanPathHeuristic(IPathHeuristic next, Func<float, float, float> blendOperator)
        {
            if(next != null && blendOperator == null)
                throw new ArgumentException("especify both next and operator.");

            this.next = next;
            this.blendOperator = blendOperator;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public float EstimatePathCost(Vector2 from, Vector2 to)
        {
            float estimate = Vector2.Distance(from,  to);

            if(next != null)
            {
                estimate = blendOperator(estimate, next.EstimatePathCost(from, to));
            }
            return estimate;
        }
    }
}
